
package net.suncaper.demo.service;


import net.suncaper.demo.domain.*;
import net.suncaper.demo.mapper.ShopcartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopcartServiceImpl implements ShopcartService{

    @Autowired
    private ShopcartMapper shopcartMapper;
    @Autowired
    private ProductService productService;

    @Override
    public void saveAddShopcartMes(Shopcart shopcart) {//增加
        shopcartMapper.insert(shopcart);
    }

    @Override
    public void delShopcartBySIdMes(int shopcartId) {//删除
        ShopcartExample shopcartExample=new ShopcartExample();
        shopcartExample.createCriteria().andShopcartIdEqualTo(shopcartId);
        shopcartMapper.deleteByExample(shopcartExample);
    }

    @Override
    public void saveUpdateShopcartMes(Shopcart shopcart) {//保存修改

        if(shopcart.getShopcartId()==null||shopcart.getShopcartId().equals("")){
            System.out.println("ERROR");
        }else{
            shopcartMapper.updateByPrimaryKey(shopcart);
        }

    }

    @Transactional
    public Shopcart findShopcartBySId(int shopcartId){

        return shopcartMapper.selectByPrimaryKey(shopcartId);
    }

    @Override
    public List<Shopcart> findShopcart(){ //显示购物车列表

        return shopcartMapper.selectByExample(new ShopcartExample());
    }

    @Override
    public List<Shopcart> findShopcartByUId(int userId){//通过user_id查找购物车
        ShopcartExample shopcartExample = new ShopcartExample();
        if(userId != 0 ){
            shopcartExample.createCriteria().andUserIdEqualTo(userId);
        }
        return shopcartMapper.selectByExample(shopcartExample);
    }


    @Override
    public void editShopcartMes(Shopcart shopcart) {
        ShopcartExample shopcartExample = new ShopcartExample();
    }

    @Override
    public void increaseNum(Integer shopcart_id){
        ShopcartExample shopcartExample = new ShopcartExample();
        ShopcartExample.Criteria criteria = shopcartExample.createCriteria();
        criteria.andShopcartIdEqualTo(shopcart_id);
        List<Shopcart> shopcarts = shopcartMapper.selectByExample(shopcartExample);
        shopcarts.get(0).setShopcartId(shopcarts.get(0).getShopcartId());
        Product product = productService.findProductById(shopcarts.get(0).getProductId());
        if(shopcarts.get(0).getShopcartNum()<product.getProductNum()){
            shopcarts.get(0).setShopcartNum(shopcarts.get(0).getShopcartNum()+1);
        }else{
            shopcarts.get(0).setShopcartNum(product.getProductNum());
        }
        shopcartMapper.updateByPrimaryKey(shopcarts.get(0));
    }

    @Override
    public void decreaseNum(Integer shopcart_id){
        ShopcartExample shopcartExample = new ShopcartExample();
        ShopcartExample.Criteria criteria = shopcartExample.createCriteria();
        criteria.andShopcartIdEqualTo(shopcart_id);
        List<Shopcart> shopcarts = shopcartMapper.selectByExample(shopcartExample);
        shopcarts.get(0).setShopcartId(shopcarts.get(0).getShopcartId());
        if(shopcarts.get(0).getShopcartNum()>1){
            shopcarts.get(0).setShopcartNum(shopcarts.get(0).getShopcartNum()-1);
        }else{
            shopcarts.get(0).setShopcartNum(1);
        }

        shopcartMapper.updateByPrimaryKey(shopcarts.get(0));
    }
//    public  void fill(List<Order> os){
//        for(Order o:os){
//            fill(o);
//        }
//    }

//    @Override
//    public void fill(Order order) {//点击下单按钮，将购物车的值传送到订单
//        ShopcartExample shopcartExample=new ShopcartExample();
//        shopcartExample.createCriteria().andShopcartIdEqualTo(order.getOrderId());
//        shopcartExample.setOrderByClause("order_id desc");
//        List<Shopcart> shopcarts=shopcartMapper. selectByExample(shopcartExample);
//        setProduct(shopcarts);
//
//        double total=0;
//        int totalNumber=0;
//        for(Shopcart shopcart:shopcarts){
//            total+=shopcart.getShopcartNum()*testclass.getProduct().getProductPrice();
//            totalNumber+=shopcart.getShopcartNum();
//        }
//        testclass.setTotal(total);
//       // order.getOrderNum()
//        testclass.setTotalNumber(totalNumber);//************此处是订单数量
//        testclass.setShopcarts(shopcarts);
//
//
//    }

//    private void setProduct(List<Shopcart> shopcarts) {
//        for(Shopcart shopcart:shopcarts){
//            setProduct(shopcart);//此处可能出错
//        }
//    }

//    private void setProduct(Shopcart shopcart) {
//        Product product=productService.get(shopcart.getProductId());//此处get修改成了静态
//        testclass.setProduct(product);
//    }


}
